package com.neusoft.neumooc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neusoft.core.common.BaseController;
import com.neusoft.core.util.Util;
import com.neusoft.neumooc.VO.GoodsKindVO;
import com.neusoft.neumooc.VO.GoodsVO;
import com.neusoft.neumooc.service.GoodsService;

@Controller
@RequestMapping(value = "/goods")
public class GoodsController extends BaseController {
	@Autowired
	GoodsService goodsService;

	@RequestMapping(value = "/getGoodsKind")
	@ResponseBody
	public Map<String, Object> index(HttpServletRequest request,
			HttpServletResponse response, String pageNo, String pageSize)
			throws Exception {
		Map<String, Object> resMap = new HashMap<String, Object>();
		int pageIndex = (Util.parseInt(pageNo) - 1) * Util.parseInt(pageSize);
		try {
			List<GoodsKindVO> goodsKind = goodsService.getGoodsKind(pageIndex,
					Util.parseInt(pageSize));
			int countNum = goodsService.queryGoodsKindCount();
			resMap.put("countNum", countNum);
			resMap.put("goodsKind", goodsKind);
		} catch (Exception e) {
			log.error("获取数据有异常信息抛出:" + e.getMessage(), e.getCause());
		}
		return resMap;
	}

	@RequestMapping(value = "/getGoods")
	@ResponseBody
	public Map<String, Object> getGoods(HttpServletRequest request,
			HttpServletResponse response, String kindId, String pageNo,
			String pageSize) throws Exception {
		Map<String, Object> resMap = new HashMap<String, Object>();
		int pageIndex = (Util.parseInt(pageNo) - 1) * Util.parseInt(pageSize);
		try {
			List<GoodsVO> goods = goodsService.getGoodsList(kindId, pageIndex,
					Util.parseInt(pageSize));
			int countNum = goodsService.getGoodsCount(kindId);
			resMap.put("countNum", countNum);
			resMap.put("goods", goods);
		} catch (Exception e) {
			log.error("获取数据有异常信息抛出:" + e.getMessage(), e.getCause());
		}
		return resMap;
	}
}
